//
//  SmsCloudRecordCell.m

#import "SmsCloudRecordCell.h"

static NSString * SmsCloudRecordCellId = @"SmsCloudRecordCellId";
@interface SmsCloudRecordCell()
@property (nonatomic,strong) UILabel *smsCloudTimeLab;
@property (nonatomic,strong) UIView * smsCloudTableView;
@property (nonatomic,strong) UIImageView *smsCloudProductImgView;
@property (nonatomic,strong) UILabel *smsCloudProductTitleLab;
@property (nonatomic,strong) UIImageView *smsCloudCityImgView;
@property (nonatomic,strong) UILabel *smsCloudCityTitleLab;
@property (nonatomic,strong) UILabel *smsCloudPhoneCodeLab;
@property (nonatomic,strong) UIButton *smsCloudPhoneCodeCopyBtn;
@end
@implementation SmsCloudRecordCell
+ (instancetype)smsCloudRecordCell:(UITableView *)tableView{
    SmsCloudRecordCell * smsCloudRecordCell = [tableView dequeueReusableCellWithIdentifier:SmsCloudRecordCellId];
    if (!smsCloudRecordCell) {
        smsCloudRecordCell = [[SmsCloudRecordCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SmsCloudRecordCellId];
    }
    smsCloudRecordCell.backgroundColor = [UIColor clearColor];
    smsCloudRecordCell.selectionStyle = UITableViewCellSelectionStyleNone;
    smsCloudRecordCell.accessoryType = UITableViewCellAccessoryNone;
    return smsCloudRecordCell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self makeSubView];
    }
    return self;
}
- (void)makeSubView{
    self.smsCloudTimeLab = [[UILabel alloc] init];
    self.smsCloudTimeLab.textColor = [SmsCloudHead SmsCloudColorHexString:@"#666666"];
    self.smsCloudTimeLab.font = [UIFont systemFontOfSize:[SmsCloudHead SmsCloudScreewRatio:24] weight:UIFontWeightRegular];
    [self addSubview:self.smsCloudTimeLab];
    [self.smsCloudTimeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([SmsCloudHead SmsCloudScreewRatio:23]));
        make.top.equalTo(@([SmsCloudHead SmsCloudScreewRatio:25]));
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:34]));
    }];
    self.smsCloudTableView = [[UIView alloc] init];
    self.smsCloudTableView.backgroundColor = [SmsCloudHead SmsCloudColorHexString:@"#F5F9FC"];
    self.smsCloudTableView.layer.cornerRadius = [SmsCloudHead SmsCloudScreewRatio:20];
    self.smsCloudTableView.clipsToBounds = true;
    [self.contentView addSubview:self.smsCloudTableView];
    [self.smsCloudTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([SmsCloudHead SmsCloudScreewRatio:24]));
        make.right.equalTo(@(-[SmsCloudHead SmsCloudScreewRatio:24]));
        make.top.equalTo(self.smsCloudTimeLab.mas_bottom).offset([SmsCloudHead SmsCloudScreewRatio:12]);
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:262]));
    }];
    UILabel * smsCloudPlaLab = [[UILabel alloc] init];
    smsCloudPlaLab.text = @"注册平台";
    smsCloudPlaLab.textColor = [SmsCloudHead SmsCloudColorHexString:@"#444444"];
    smsCloudPlaLab.font = [UIFont systemFontOfSize:[SmsCloudHead SmsCloudScreewRatio:30] weight:UIFontWeightRegular];
    [self.smsCloudTableView addSubview:smsCloudPlaLab];
    [smsCloudPlaLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([SmsCloudHead SmsCloudScreewRatio:23]));
        make.top.equalTo(@([SmsCloudHead SmsCloudScreewRatio:25]));
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:34]));
    }];
    self.smsCloudProductImgView = [[UIImageView alloc] init];
    [self.smsCloudTableView addSubview:self.smsCloudProductImgView];
    [self.smsCloudProductImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([SmsCloudHead SmsCloudScreewRatio:199]));
        make.centerY.equalTo(smsCloudPlaLab);
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:34]));
        make.width.equalTo(@([SmsCloudHead SmsCloudScreewRatio:34]));
    }];
    self.smsCloudProductTitleLab = [[UILabel alloc] init];
    self.smsCloudProductTitleLab.textColor = [SmsCloudHead SmsCloudColorHexString:@"#262729"];
    self.smsCloudProductTitleLab.font = [UIFont systemFontOfSize:[SmsCloudHead SmsCloudScreewRatio:30] weight:UIFontWeightBold];
    [self addSubview:self.smsCloudProductTitleLab];
    [self.smsCloudProductTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.smsCloudProductImgView.mas_right).offset([SmsCloudHead SmsCloudScreewRatio:15]);
        make.centerY.equalTo(smsCloudPlaLab);
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:42]));
    }];
    
    UILabel * smsCloudPhoneLab = [[UILabel alloc] init];
    smsCloudPhoneLab.text = @"手机号";
    smsCloudPhoneLab.textColor = [SmsCloudHead SmsCloudColorHexString:@"#444444"];
    smsCloudPhoneLab.font = [UIFont systemFontOfSize:[SmsCloudHead SmsCloudScreewRatio:30] weight:UIFontWeightRegular];
    [self.smsCloudTableView addSubview:smsCloudPhoneLab];
    [smsCloudPhoneLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([SmsCloudHead SmsCloudScreewRatio:23]));
        make.top.equalTo(smsCloudPlaLab.mas_bottom).offset([SmsCloudHead SmsCloudScreewRatio:38]);
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:34]));
    }];
    self.smsCloudCityImgView = [[UIImageView alloc] init];
    [self.smsCloudTableView addSubview:self.smsCloudCityImgView];
    [self.smsCloudCityImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([SmsCloudHead SmsCloudScreewRatio:199]));
        make.centerY.equalTo(smsCloudPhoneLab);
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:34]));
        make.width.equalTo(@([SmsCloudHead SmsCloudScreewRatio:34]));
    }];
    self.smsCloudCityTitleLab = [[UILabel alloc] init];
    self.smsCloudCityTitleLab.textColor = [SmsCloudHead SmsCloudColorHexString:@"#262729"];
    self.smsCloudCityTitleLab.font = [UIFont systemFontOfSize:[SmsCloudHead SmsCloudScreewRatio:30] weight:UIFontWeightBold];
    self.smsCloudCityTitleLab.numberOfLines = 0;
    [self addSubview:self.smsCloudCityTitleLab];
    [self.smsCloudCityTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.smsCloudCityImgView.mas_right).offset([SmsCloudHead SmsCloudScreewRatio:15]);
        make.right.equalTo(@(-[SmsCloudHead SmsCloudScreewRatio:144]));
        make.centerY.equalTo(smsCloudPhoneLab);
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:118]));
    }];
    UIButton * smsCloudPhoneCopyBtn = [[UIButton alloc] init];
    [smsCloudPhoneCopyBtn setBackgroundImage:[SmsCloudHead SmsCloudImageNamed:@"smsCloudPhoneCopy"] forState:UIControlStateNormal];
    [smsCloudPhoneCopyBtn addTarget:self action:@selector(smsCloudPhoneCopy) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:smsCloudPhoneCopyBtn];
    [smsCloudPhoneCopyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-[SmsCloudHead SmsCloudScreewRatio:24]));
        make.centerY.equalTo(smsCloudPhoneLab);
        make.width.equalTo(@([SmsCloudHead SmsCloudScreewRatio:102]));
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:58]));
    }];
    
    UILabel * smsCloudPhoneCodeLab = [[UILabel alloc] init];
    smsCloudPhoneCodeLab.text = @"验证码";
    smsCloudPhoneCodeLab.textColor = [SmsCloudHead SmsCloudColorHexString:@"#444444"];
    smsCloudPhoneCodeLab.font = [UIFont systemFontOfSize:[SmsCloudHead SmsCloudScreewRatio:30] weight:UIFontWeightRegular];
    [self.smsCloudTableView addSubview:smsCloudPhoneCodeLab];
    [smsCloudPhoneCodeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([SmsCloudHead SmsCloudScreewRatio:23]));
        make.top.equalTo(smsCloudPhoneLab.mas_bottom).offset([SmsCloudHead SmsCloudScreewRatio:38]);
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:34]));
    }];
    self.smsCloudPhoneCodeLab = [[UILabel alloc] init];
    self.smsCloudPhoneCodeLab.textColor = [SmsCloudHead SmsCloudColorHexString:@"#0087FF"];
    self.smsCloudPhoneCodeLab.font = [UIFont systemFontOfSize:[SmsCloudHead SmsCloudScreewRatio:30] weight:UIFontWeightBold];
    [self addSubview:self.smsCloudPhoneCodeLab];
    [self.smsCloudPhoneCodeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.smsCloudCityImgView.mas_right).offset([SmsCloudHead SmsCloudScreewRatio:15]);
        make.centerY.equalTo(smsCloudPhoneCodeLab);
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:42]));
    }];
    self.smsCloudPhoneCodeCopyBtn = [[UIButton alloc] init];
    self.smsCloudPhoneCodeCopyBtn.hidden = true;
    [self.smsCloudPhoneCodeCopyBtn setBackgroundImage:[SmsCloudHead SmsCloudImageNamed:@"smsCloudPhoneCopy"] forState:UIControlStateNormal];
    [self.smsCloudPhoneCodeCopyBtn addTarget:self action:@selector(smsCloudPhoneCodeCopy) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.smsCloudPhoneCodeCopyBtn];
    [self.smsCloudPhoneCodeCopyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-[SmsCloudHead SmsCloudScreewRatio:24]));
        make.centerY.equalTo(self.smsCloudPhoneCodeLab);
        make.width.equalTo(@([SmsCloudHead SmsCloudScreewRatio:102]));
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:58]));
    }];
}
- (void)smsCloudPhoneCopy{
    NSString *smsCloudPhoneContent = [_smsCloudDic objectForKey:@"phone"];
    if(smsCloudPhoneContent.length){
        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
        pasteboard.string = smsCloudPhoneContent;
        [SVProgressHUD showInfoWithStatus:@"复制成功"];
    }
}
- (void)smsCloudPhoneCodeCopy{
    NSString *smsCloudPhoneCodeContent = [_smsCloudDic objectForKey:@"phoneCode"];
    if(smsCloudPhoneCodeContent.length){
        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
        pasteboard.string = smsCloudPhoneCodeContent;
        [SVProgressHUD showInfoWithStatus:@"复制成功"];
    }
}

-(void)setSmsCloudDic:(NSMutableDictionary *)smsCloudDic{
    _smsCloudDic = smsCloudDic;
    if(_smsCloudDic){
        self.smsCloudTimeLab.text = [_smsCloudDic objectForKey:@"time"];
        [self.smsCloudProductImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://www.ydl.cool/img/static/sms/%@0.webp",[_smsCloudDic objectForKey:@"productCode"]]] placeholderImage:[SmsCloudHead SmsCloudImageNamed:@"smsCloudLogo"]];
        self.smsCloudProductTitleLab.text = [_smsCloudDic objectForKey:@"productName"];
        [self.smsCloudCityImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://www.ydl.cool/img/static/guojia/guojia01/%@.png",[_smsCloudDic objectForKey:@"cityCode"]]] placeholderImage:[SmsCloudHead SmsCloudImageNamed:@"smsCloudLogo"]];
        self.smsCloudCityTitleLab.text = [NSString stringWithFormat:@"+%@ (%@)",[_smsCloudDic objectForKey:@"phone"],[_smsCloudDic objectForKey:@"cityName"]];
        if(![_smsCloudDic objectForKey:@"phoneCode"]){
            self.smsCloudPhoneCodeLab.text = @"获取中";
            [self getSmsCloudPhoneCode];
            self.smsCloudPhoneCodeCopyBtn.hidden = true;
        }else{
            self.smsCloudPhoneCodeCopyBtn.hidden = false;
            self.smsCloudPhoneCodeLab.text = [_smsCloudDic objectForKey:@"phoneCode"];
        }
    }
}
- (void)getSmsCloudPhoneCode{
    __weak __typeof__(self) weakSelf = self;
    [SmsCloudDataHelper requestSmsCloudPhoneCode:[_smsCloudDic objectForKey:@"cityCode"] withPhone:[_smsCloudDic objectForKey:@"phone"] withProductCode:[_smsCloudDic objectForKey:@"productCode"] scuessBlock:^(NSString * _Nonnull smsCloudPhone) {
        if (smsCloudPhone&&smsCloudPhone.length > 0) {
            self.smsCloudPhoneCodeCopyBtn.hidden = false;
            self.smsCloudPhoneCodeLab.text = smsCloudPhone;
            weakSelf.smsCloudPhoneCodeLab.text = smsCloudPhone;
            [weakSelf.smsCloudDic setObject:smsCloudPhone forKey:@"phoneCode"];
            if(weakSelf.SmsCloudRecordPhoneCodeBlock){
                weakSelf.SmsCloudRecordPhoneCodeBlock(weakSelf.smsCloudDic);
            }
        }else{
            [weakSelf performSelector:@selector(getSmsCloudPhoneCode) withObject:self afterDelay:5.0];
        }
    }];
}

@end
